package com.ht.api.call.func.impl.wwx;

import cn.hutool.json.JSONObject;
import com.ht.api.bean.AjaxResult;
import com.ht.api.bean.LoginBean;
import com.ht.api.call.func.IFunctionCall;
import com.ht.api.db.Db;
import com.ht.api.db.DbList;
import com.ht.api.db.Entity;

/**
 * 社群客户查询后置函数
 */
public class WwxGroupChatCustomerQueryFunctionCall implements IFunctionCall {

    @Override
    public String getName() {
        return "社群客户查询后置函数";
    }

    @Override
    public JSONObject execute(LoginBean me, JSONObject param, AjaxResult result) {
        DbList list = result.getRecords();

        for (Entity customer : list) {
            String userid = customer.getStr("external_userid");
            int type = customer.getInt("type");

            if (type == 1) {
                String sql = "select oneid,nickname,aliasname,avatar,gender from user_main where status = 0 and bitmap_contains(users, "+Math.abs(userid.hashCode())+")";

                Entity user = Db.findOne(me.getDataSource(), sql);
                customer.set("mapper_oneid", user);
            } else {
                Entity user = Db.createSqlParser("wx_workuser").select("alias aliasname,avatar,gender,name,userid").where("userid", userid).findOne(me.getDataSource());
                customer.set("mapper_oneid", user);
            }

        }

        return null;
    }
}
